package utilities.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import logic.PayrollRecord;

/**
 * 
 * @author Geoffrey Shaw, Kristofer Deviat
 *
 * This class provides all the utilities needed to write a csv file.
 */
public class CSVFileOut {
	private char delimiter = ';';
	private BufferedWriter bw;
	
	public CSVFileOut(String f) throws IOException {
		File file = new File(f);
		FileWriter fw = new FileWriter(file);
		
		if (!file.exists()) {
			file.createNewFile();
		}
		
		bw = new BufferedWriter(fw);
	}
		
	/**
	 * sets the delimeter to be used in the file
	 * @param d char delimeter
	 */
	public void setDelimiter(char d){
		this.delimiter = d;
	}
	
	/**
	 * adds a payroll record to the csv file.
	 * @param p payroll record to be added
	 * @return returns true or false if the record was added.
	 * @throws IOException
	 */
	public boolean addElement(PayrollRecord p) throws IOException {
		bw.write(p.payrollCSV(delimiter) + "\n");
		return true;
	}
	
	/**
	 * closes the csv file and releases all resources to the operating system.
	 * @throws IOException
	 */
	public void close() throws IOException {
		bw.close();
	}
}
